﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace DynamicChart.UI.Dialogs
{
    public partial class SelectYearWindow : ChildWindow
    {
        public int SelectedYear { get; private set; }

        private int _startYear;
        private int _selYear;

        public SelectYearWindow()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(SelectYearWindow_Loaded);
        }

        void SelectYearWindow_Loaded(object sender, RoutedEventArgs e)
        {
            _selYear = DateTime.Now.Year;
            _startYear = _selYear - _selYear % 10;
            Relayout();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void _ctlBtnPrev_Click(object sender, RoutedEventArgs e)
        {
            _startYear = _startYear - 10;
            Relayout();
        }

        private void _ctlBtnNext_Click(object sender, RoutedEventArgs e)
        {
            _startYear = _startYear + 10;
            Relayout();
        }

        private void Relayout()
        {
            _ctlPanelYearList.Children.Clear();
            for (int index = 0; index < 10; index++)
            {
                HyperlinkButton btn = new HyperlinkButton();
                btn.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
                btn.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
                btn.BorderThickness = new Thickness(1);
                btn.BorderBrush = new SolidColorBrush(Colors.Blue);
                btn.Content = (_startYear + index).ToString();
                btn.Tag = _startYear + index;
                btn.IsTabStop = false;
                Grid.SetRow(btn, index / 5);
                Grid.SetColumn(btn, index % 5);
                _ctlPanelYearList.Children.Add(btn);
                btn.Click += new RoutedEventHandler(btn_Click);

                if (_selYear == _startYear + index)
                {
                    btn.FontWeight = FontWeights.Bold;
                }
            }
            _ctlLabel.Text = string.Format("{0}-{1}", _startYear, _startYear + 9);
            _ctlBtnPrev.Content = string.Format("...{0}", _startYear);
            _ctlBtnNext.Content = string.Format("{0}...", _startYear + 9);
        }

        void btn_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton btn = sender as HyperlinkButton;
            SelectedYear = Convert.ToInt32(btn.Tag);
            DialogResult = true;
        }
    }
}

